package com.rc.saas.tenant.util;

import com.rc.saas.tenant.common.utils.EncodeUtil;
import com.rc.saas.tenant.common.utils.HMacMD5;
import com.rc.saas.tenant.common.vo.StdRequest;
import com.rc.saas.tenant.model.hlhtsk.HlhtskTenant;

/**
 * 签名和加密工具类
 */
public class PubFetchTool {
    public static String genDDDecodeData(HlhtskTenant config, String jsonData) {
        return EncodeUtil.decrypt(jsonData, getAESSecret(config), getAESLV(config));
    }

    public static String genDDEncodeData(HlhtskTenant config, String jsonData) {
        return EncodeUtil.encrypt(jsonData, getAESSecret(config), getAESLV(config));
    }

    public static String genDDSig(HlhtskTenant config, String enString, String timeStamp) {
        return HMacMD5.getHmacMd5Str(getSigSecret(config), config.getHlhtTenantCode() + enString + timeStamp + "0001");
    }

    public static String getAESSecret(HlhtskTenant config) {
        return config.getDataSecret();
    }

    public static String getAESLV(HlhtskTenant config) {
        return config.getDataSecretiv();
    }

    public static String getSigSecret(HlhtskTenant config) {
        return config.getSigSecret();
    }

    public static boolean checkSig(HlhtskTenant config, StdRequest request) {
        String Data = request.getData();
        String TimeStamp = request.getTimeStamp();
        String Seq = request.getSeq();
        String Sig = request.getSig();

        String ddSig = HMacMD5.getHmacMd5Str(getSigSecret(config), config.getHlhtTenantCode() + Data + TimeStamp + Seq);
        if (Sig.equals(ddSig)) {
            return true;
        }
        return false;
    }

}
